package com.carrental.vehicle.controller;

import com.carrental.common.api.CommonResult;
import com.carrental.common.api.PageResult;
import com.carrental.vehicle.service.VehicleUsageService;
import com.carrental.vehicle.vo.VehicleUsageStatVO;
import com.carrental.vehicle.vo.VehicleUsageDetailVO;
import com.carrental.vehicle.vo.VehicleWearVO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 车辆使用统计控制器
 */
@RestController
@RequestMapping("/vehicle/usage")
public class VehicleUsageController {

    @Autowired
    private VehicleUsageService vehicleUsageService;

    /**
     * 分页获取车辆使用统计数据
     */
    @GetMapping("/statistics")
    public CommonResult<PageResult<VehicleUsageStatVO>> getStatistics(
            @RequestParam(value = "current", defaultValue = "1") Integer current,
            @RequestParam(value = "size", defaultValue = "10") Integer size,
            @RequestParam(value = "licensePlate", required = false) String licensePlate,
            @RequestParam(value = "brandId", required = false) Long brandId,
            @RequestParam(value = "type", required = false) Integer type,
            @RequestParam(value = "startDate", required = false) String startDate,
            @RequestParam(value = "endDate", required = false) String endDate) {
        
        PageResult<VehicleUsageStatVO> result = vehicleUsageService.getVehicleUsageStatistics(
                current, size, licensePlate, brandId, type, startDate, endDate);
                
        return CommonResult.success(result);
    }

    /**
     * 获取车辆使用详情
     */
    @GetMapping("/detail/{vehicleId}")
    public CommonResult<VehicleUsageDetailVO> getDetail(@PathVariable Long vehicleId) {
        VehicleUsageDetailVO detail = vehicleUsageService.getVehicleUsageDetail(vehicleId);
        return CommonResult.success(detail);
    }

    /**
     * 获取车辆损耗评估
     */
    @GetMapping("/wear/{vehicleId}")
    public CommonResult<VehicleWearVO> getWearAnalysis(@PathVariable Long vehicleId) {
        VehicleWearVO wear = vehicleUsageService.getVehicleWearAnalysis(vehicleId);
        return CommonResult.success(wear);
    }

    /**
     * 导出车辆使用统计数据
     */
    @GetMapping("/export")
    public void exportStatistics(
            HttpServletResponse response,
            @RequestParam(value = "licensePlate", required = false) String licensePlate,
            @RequestParam(value = "brandId", required = false) Long brandId,
            @RequestParam(value = "type", required = false) Integer type,
            @RequestParam(value = "startDate", required = false) String startDate,
            @RequestParam(value = "endDate", required = false) String endDate) {
        
        vehicleUsageService.exportVehicleUsageStatistics(
                response, licensePlate, brandId, type, startDate, endDate);
    }

    /**
     * 生成车辆损耗评估报告
     */
    @GetMapping("/wear/report/{vehicleId}")
    public void generateWearReport(HttpServletResponse response, @PathVariable Long vehicleId) {
        vehicleUsageService.generateVehicleWearReport(response, vehicleId);
    }
} 